package com.jinxin.platform.cdsmsmgr.controller;

import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.pojo.domains.AliSmsSignInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsSignInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.domains.TxSmsSignInfo;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import com.jinxin.platform.cdsmsmgr.pojo.entity.ResultEntity;
import com.jinxin.platform.cdsmsmgr.pojo.vo.SignInfo;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISmsSignService;
import com.jinxin.platform.cdsmsmgr.utils.UploadFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-06-29
 * @Author: Lingnan
 **/
@APCOSApi(value = "签名管理接口", developers = {"那个男人"}, description = "签名管理接口")
@RestController
@RequestMapping("/cdsmsmgr/smsSign")
@Slf4j
public class SmsSignController {
    @Autowired
    private ISmsSignService signService;
    @Autowired
    private SysUserRpc sysUserRpc;
    @Value("${platform.upload}")
    private String uploadPath;

    @APCOSApiOperation(value = "分页获取签名", developers = {"lingnan"}, description = "分页获取签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam long page, @RequestParam long limit,
                                   @RequestParam(name = "name",required = false)String name, @RequestParam(name = "status",required = false)Integer status,
                                   @RequestParam(name = "accountType") Integer accountType){
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if (currentUser == null) {
            log.error("未登录--,smsSign list接口");
            return null;
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("signName", name);
        param.put("status", status);
        String userAccount = currentUser.getUserAccount();
        if(!Constant.ACCOUNT_ADMIN.equals(userAccount)){
            param.put("applyUser", userAccount);
        }
        param.put("accountType", accountType);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", signService.listPageCount(param));
        resultMap.put("data", signService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "保存或更新签名", developers = {"lingnan"}, description = "保存或更新签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/saveInfo")
    public ResultEntity saveInfo(SmsSignInfo signInfo){
        return signService.saveOrUpdate(signInfo);
    }

    @APCOSApiOperation(value = "删除签名", developers = {"lingnan"}, description = "删除签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/delete")
    public ResultEntity delete(String id) {
        return signService.deleteById(id);
    }

    @APCOSApiOperation(value = "根据主键查询签名证明图片", developers = {"lingnan"}, description = "根据主键查询签名证明图片",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/queryImgsById")
    public JsonResult queryImgsById(String id) {
        return signService.queryById(id);
    }

    @APCOSApiOperation(value = "上传文件接口", developers = {"lingnan"}, description = "上传文件接口(非真传)",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/fakeUploadFile")
    public Map<String,Object> fakeUploadFiles(MultipartFile file) {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("msg","success");
        resultMap.put("filePath","");
        resultMap.put("fileName",file.getOriginalFilename());
        return resultMap;
    }

    @APCOSApiOperation(value = "上传文件", developers = {"lingnan"}, description = "上传文件",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/uploadFiles")
    public Map<String,Object> uploadFiles(MultipartFile file) {
        String filePath = UploadFileUtil.uploadFile(file, this.uploadPath);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("msg","success");
        resultMap.put("filePath",filePath);
        resultMap.put("fileName",file.getOriginalFilename());
        return resultMap;
    }

    @APCOSApiOperation(value = "添加签名", developers = {"lingnan"}, description = "添加签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/addSmsSign")
    public ResultEntity appSmsSign(SmsSignInfo signInfo){
        return signService.addSmsSign(signInfo);
    }

    @APCOSApiOperation(value = "申请或修改阿里签名", developers = {"lingnan"}, description = "申请或修改阿里签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/applyAliSmsSign")
    public JsonResult applyAliSmsSign(@Valid @RequestBody AliSmsSignInfo smsSignInfo, BindingResult result){
        if (result.hasErrors()){
            // 如果数据验证有误,返回第一条错误信息到前端
            return JsonResult.error(result.getFieldError().getDefaultMessage());
        }
        return signService.applyAliSmsSign(smsSignInfo);
    }

    @APCOSApiOperation(value = "申请或修改腾讯签名", developers = {"lingnan"}, description = "申请或修改腾讯签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/applyTxSmsSign")
    public JsonResult applyTxSmsSign(@Valid @RequestBody TxSmsSignInfo smsSignInfo, BindingResult result){
        if (result.hasErrors()){
            return JsonResult.error(result.getFieldError().getDefaultMessage());
        }
        return signService.applyTxSmsSign(smsSignInfo);
    }

    @APCOSApiOperation(value = "查询同步签名列表", developers = {"lingnan"}, description = "查询同步签名列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/querySmsSignList")
    public Map<String,Object> querySmsSignList() {
        JsonResult jsonResult = signService.querySmsSignList();
        int code = 0,count = 0;
        String msg = "success";
        List<SignInfo> signInfoList = new ArrayList<>();
        if(jsonResult.getSuccess()){
            signInfoList = (List<SignInfo>) jsonResult.getData();
            count = signInfoList.size();
        }else {
            code = jsonResult.getStatus();
            msg = jsonResult.getMsg();
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("count", count);
        resultMap.put("data", signInfoList);
        return resultMap;
    }

    @APCOSApiOperation(value = "选择同步的签名", developers = {"lingnan"}, description = "选择同步的签名",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/syncSmsSign")
    public JsonResult syncSmsSign(@RequestBody List<SignInfo> signInfoList){
        return signService.syncSmsSign(signInfoList);
    }

    @APCOSApiOperation(value = "刷新签名状态", developers = {"lingnan"}, description = "刷新签名状态",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/freshStatus")
    public JsonResult freshStatus(String id) {
        return signService.freshStatus(id);
    }
}
